#!/usr/bin/python3
# coding: utf-8
# file: zipfile_.py
# 2018-06-12
import os, zipfile

def make_zip(source_dir, filter_floders=[]):
	'''文件压缩zip'''
	out_file = '{}.zip'.format(source_dir)
	zip_ = zipfile.ZipFile(out_file, 'w')
	dir_len = len(os.path.dirname(source_dir))		# 获取目录路径长度, 用于切片

	for root, dirs, files in os.walk(source_dir):
		if (root[len(source_dir):].lstrip(os.path.sep).split(os.sep)[0] in filter_floders 
			or root.split(os.sep)[-1]  in filter_floders) : continue # 过滤不希望被压缩的文件夹
		for file in files:
			if not file.endswith('.py'): continue 	# 压缩指定后缀的文件
			file = os.path.join(root, file) 		# 文件绝对路径
			arc = file[dir_len:].lstrip(os.sep) 	# 文件相对路径
			zip_.write(file, arc)
	zip_.close()


if __name__ == '__main__':
	source_dir = "D:\\WorkS\\Test"
	make_zip(source_dir, ['asdfaf', '__pycache__'])
